<?php
  //Loader::loadFile('config.php', 'modules/VoteThailand');
  /**
  * initialise module
  *
  */
  function VotePetition_init() {

    //Create votepetition_province table
    if (!DBUtil::createTable('votepetition_province')) {
        return false;
    }
    //Create votepetition_district table
    if (!DBUtil::createTable('votepetition_district')) {
        return false;
    }
    //Create votepetition_province table
    if (!DBUtil::createTable('votepetition_tambon')) {
        return false;
    }
    //Create votepetition_village table
    if (!DBUtil::createTable('votepetition_village')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_house_no')) {
        return false;
    }

    //Create votepetition_party table
    if (!DBUtil::createTable('votepetition_party')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_villagepoll')) {
        return false;
    }


    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_research')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_person_in_house')) {
        return false;
    }
/*
    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_career')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_career_problem')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_career_solve')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_income')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_outlay')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_general_problem')) {
        return false;
    }

    //Create votethailandpollcenter_person_canvasser table
    if (!DBUtil::createTable('votepetition_general_solve')) {
        return false;
    }
*/
    VotePetition_init_data();
    VotePetition_init_folder();
    return true;
  }


  function VotePetition_init_folder(){

    if (!is_dir("pnTemp/votepetition_upload/")) {
      mkdir("pnTemp/votepetition_upload/", 0755);
    }
    
  }

  function VotePetition_delete() {

    DBUtil::dropTable('votepetition_province');
    DBUtil::dropTable('votepetition_district');
    DBUtil::dropTable('votepetition_tambon');
    DBUtil::dropTable('votepetition_village');
    DBUtil::dropTable('votepetition_house_no');
    DBUtil::dropTable('votepetition_party');
    DBUtil::dropTable('votepetition_villagepoll');
    DBUtil::dropTable('votepetition_research');

    DBUtil::dropTable('votepetition_person_in_house');
/*    DBUtil::dropTable('votepetition_career');
    DBUtil::dropTable('votepetition_career_problem');
    DBUtil::dropTable('votepetition_career_solve');
    DBUtil::dropTable('votepetition_income');
    DBUtil::dropTable('votepetition_outlay');
    DBUtil::dropTable('votepetition_general_problem');
    DBUtil::dropTable('votepetition_general_solve');
*/
/*
    DROP TABLE `zk_votepetition_province`;
    DROP TABLE `zk_votepetition_district`;
    DROP TABLE `zk_votepetition_tambon`;
    DROP TABLE `zk_votepetition_village`;
    DROP TABLE `zk_votepetition_party`;
    DROP TABLE `zk_votepetition_poll`;
*/
    return true;
  }

   function VotePetition_init_data(){
    setlocale ( LC_ALL, 'en_US.UTF-8' );
    //Init province data
    $file = "modules/VotePetition/data/province.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $objArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $provinceArray[] = array(
                                    'id'        => $data[0],
                                    'name'      => VotePetition_fixEncoding($data[1]),
                                    'latitude'  => $data[2],
                                    'longitude' => $data[3]
                                  );
      }
      DBUtil::insertObjectArray($provinceArray, 'votepetition_province', true);
      unset($provinceArray);
      unset($data);
      fclose($handle);
    }

    //Init district data
    $file = "modules/VotePetition/data/district.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $districtArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $districtArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'name'      => $data[2],
                                      'latitude'  => $data[3],
                                      'longitude' => $data[4]
                                    );
      }
      //DBUtilEx::setCharacterSet('utf8');
      DBUtil::insertObjectArray($districtArray, 'votepetition_district', true);
      unset($districtArray);
      unset($data);
      fclose($handle);
    }


    //Init tambon data
    $file = "modules/VotePetition/data/tambon.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $tambonArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $tambonArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'name'      => $data[3],
                                      'latitude'  => $data[4],
                                      'longitude' => $data[5]
                                    );
      }
      DBUtil::insertObjectArray($tambonArray, 'votepetition_tambon', true);
      unset($tambonArray);
      unset($data);
      fclose($handle);
    }

    //Init village data
    $file = "modules/VotePetition/data/village.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $villageArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $villageArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'tam_id'    => $data[3],
                                      'moo'       => $data[4],
                                      'name'      => $data[5],
                                      'latitude'  => $data[6],
                                      'longitude' => $data[7]
                                    );
      }
      DBUtil::insertObjectArray($villageArray, 'votepetition_village', true);
      unset($villageArray);
      unset($data);
      fclose($handle);
    }

  }

  function VotePetition_fixEncoding($in_str) {
    $cur_encoding = mb_detect_encoding($in_str) ;

    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
      return $in_str;
    else
      return utf8_encode($in_str);
  } 

